use libc;
use io_uring::{opcode, types, IoUring};

use std::os::unix::io::AsRawFd;
use std::{fs, io};
use std::str::from_utf8;


fn main() {
    let mut ring = IoUring::new(32).unwrap();
    let fd = fs::File::open("data.txt").unwrap();

    // 要读入数据的缓冲区。
    let mut buf = vec![0; 1024];

    let mut vec = Vec::new();
    let v = libc::iovec {
        iov_base: buf.as_mut_ptr() as *mut libc::c_void,
        iov_len: 1024,
    };
    vec.push(v);
    // 创建一个sqe
    let e = opcode::Readv::new(types::Fd(fd.as_raw_fd()), vec.as_ptr(), 1).build().user_data(0x42);

    unsafe {
        ring.submission()
            .push(&e)
            .expect("sq full");
    }

    // 等待IO完成。
    ring.submit_and_wait(1).unwrap();

    let cqe = ring.completion().next().expect("empty");
    assert_eq!(cqe.user_data(), 0x42);

    println!("Result is {}", from_utf8(&buf).unwrap());
}
